<template>
	<view class="back-view" @touchmove.stop.prevent="moveHandle">
		<view class="view-background"></view>
		<view class="white-view-back">
			<view class="top-back">
				<view class="top-name">{{pageTitle}}</view>
				<view class="close-back" @click="closeBtnAction">
					<image class="close-img" src="../../../static/close.png"></image>
				</view>
			</view>

			<view class="top-line"></view>
			<view style="width: 100%;">
				<uni-forms ref="baseForm" :modelValue="baseFormData">
					<uni-forms-item label="角色名称：" labelWidth="100" labelAlign="right" required>
						<uni-easyinput v-model="baseFormData.name" placeholder="请填写" />
					</uni-forms-item>
					<uni-forms-item label="角色权限：" labelWidth="100" labelAlign="right" required>
						<scroll-view scroll-y="true" class="scroll-view-back">
							<ly-tree ref="tree" :tree-data="treeData" show-checkbox node-key="id"
								@node-click="handleNodeClick" />
						</scroll-view>
					</uni-forms-item>
				</uni-forms>
			</view>
			<view class="bottom-back">
				<view class="bottom-btn bottom-btn-cancel" @click="closeBtnAction">取消</view>
				<view class="bottom-btn" @click="submitAction">确定</view>
			</view>
		</view>
	</view>
</template>

<script>
	// https://ext.dcloud.net.cn/plugin?id=1000
	import LyTree from '@/components/ly-tree/ly-tree.vue'
	export default {
		components: {
			LyTree
		},
		props: {

			roleID: {
				type: String,
				default: '',
			},
			editType: {
				type: String,
				default: 'add',
			},

		},
		data() {
			return {
				pageTitle: "新增角色",
				baseFormData: {
					'id': '',
					'name': '',
				},
				treeData: []
			}
		},
		mounted() {
			if (this.editType == 'edit') {
				this.pageTitle = '编辑角色';
				this.requestInfoData();
				this.requestAuthorInfoData();
			}
			this.requestAuthorData();
		},
		methods: {
			//信息网络请求
			async requestInfoData() {
				let _this = this
				const res = await this.$myRequest({
					url: '/oauth/role/' + this.roleID,
					method: 'GET',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					_this.baseFormData = res.data.data;



					// 		//常用律师
					// 		var lawyerIDs = _this.baseFormData.lawyers;
					// 		for (var i = 0; i < _this.lawyerList.length; i++) {
					// 			var value = _this.lawyerList[i];
					// 			for (var j = 0; j < lawyerIDs.length; j++) {
					// 				var item = lawyerIDs[j];
					// 				if (item.account.id == value.accId) {
					// 					_this.lawyerSelectedArr.push(value)
					// 				}
					// 			}
					// 		}
				}
			},

			//获取指定角色的权限
			async requestAuthorInfoData() {
				let _this = this
				const res = await this.$myRequest({
					url: "/oauth/resource/list/" + this.roleID,
					method: 'GET',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					// _this.treeData = res.data.data;
					// console.log(_this.roleArr)
				}
			},
			//权限数结构
			async requestAuthorData() {
				let _this = this
				const res = await this.$myRequest({
					url: "/oauth/resource/list/tree",
					method: 'POST',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					_this.treeData = res.data.data;
					console.log(_this.roleArr)
				}
			},



			//保存
			async submitAction() {
				var arr = this.$refs.tree.getCheckedNodes(true, true)
				this.requestRoleAuthor(arr)
				console.log(arr)
				let _this = this
				if (!this.baseFormData.name) {
					uni.showToast({
						title: '请填写角色名称',
						icon: 'none'
					})
					return;
				}
				
				if (!this.arr.length) {
					uni.showToast({
						title: '请添加角色权限',
						icon: 'none'
					})
					return;
				}

				var parms = {
					'id': this.baseFormData.id,
					'name': this.baseFormData.name,
				}

				// var url = "/oauth/role";
				// const res = await this.$myRequest({
				// 	url: url,
				// 	method: _this.editType == 'edit' ? 'PUT' : 'POST',
				// 	data: parms
				// })
				// console.log('返回结果', res)
				// if (res.data.code == 200) {
				// 	uni.showToast({
				// 		title: _this.editType == 'edit' ? '编辑成功' : '新增成功'
				// 	})
				// 	_this.$emit("successBack");
				// 	_this.closeBtnAction()
				// }
			},

			//角色配置权限
			async requestRoleAuthor(arr) {
				let _this = this
				var parms = [];
				for (var i = 0; i < arr.length; i++) {
					var item = arr[i];
					parms.push(item.id)
				}
				const res = await this.$myRequest({
					url: "/oauth/role/perm/res/" + this.roleID,
					method: 'POST',
					data: parms
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					// _this.treeData = res.data.data;
					console.log(_this.roleArr)
				}
			},


			// uni-app中emit触发的方法只能接受一个参数，所以会回传一个对象，打印对象即可见到其中的内容
			handleNodeClick(obj) {
				console.log('handleNodeClick', JSON.stringify(obj));
			},
			handleNodeExpand(obj) {
				console.log('handleNodeExpand', JSON.stringify(obj));
			},



			closeBtnAction() {
				this.$emit('addRoleClose')
			},
			moveHandle() {
				return true
			},
		},
	}
</script>

<style>
	.back-view {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 199;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.view-background {
		position: absolute;
		width: 100%;
		height: 100%;
		z-index: 100;
		background-color: rgba(0, 0, 0, 0.3);
	}

	.white-view-back {
		width: 552px;
		height: 500px;
		padding: 24px;
		max-height: 500px;
		background-color: white;
		margin-top: -50px;
		border-radius: 4px;
		z-index: 101;
		position: absolute;
	}

	.top-back {
		display: flex;
		justify-content: space-between;
	}

	.top-name {
		font-size: 18px;
		font-weight: normal;
		color: #333333;

	}

	.close-back {
		cursor: pointer;
		width: 20px;
		height: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.close-img {
		width: 12px;
		height: 12px;
	}

	.top-line {
		height: 1px;
		background: #EEEEEE;
		width: 100%;
		margin-top: 20px;
		margin-bottom: 20px;
	}

	.bottom-back {
		bottom: 0px;
		width: 100%;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		position: absolute;
		height: 60px;
		right: 16px;
	}

	.bottom-btn {
		cursor: pointer;
		width: 104px;
		height: 32px;
		background: #2DA9C5;
		border-radius: 2px;
		color: #FFFFFF;
		font-size: 14px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.bottom-btn-cancel {
		margin-right: 10px;
		background: #FFFFFF;
		border: 1px solid #EEEEEE;
		color: #333333;
	}


	.scroll-view-back {
		width: 100%;
		height: 320px;
		border: 1px #eee solid;
	}
</style>
